package com.parking.carsystem.parkingchargesystem.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.parking.carsystem.parkingchargesystem.R;
import com.parking.carsystem.parkingchargesystem.activity.MainActivity;
import com.parking.carsystem.parkingchargesystem.module.HomeTopMessageModel;

import java.util.ArrayList;

public class ViewpagerAdapter extends PagerAdapter {

    private ArrayList<HomeTopMessageModel> imageData;
    Context context;

    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    //初始化每个条目要显示的内容
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        //拿着position位置 % 集合.size
        View inflate = LayoutInflater.from(context).inflate(R.layout.item_top_image, container, false);
        int newposition = position % imageData.size();
        HomeTopMessageModel model = imageData.get(newposition);
        //获取到条目要显示的内容imageview
        ImageView imageView =  inflate.findViewById(R.id.image);
        imageView.setImageResource(model.image);
        TextView name_top = inflate.findViewById(R.id.name_top);
        TextView name_bottom = inflate.findViewById(R.id.name_bottom);
        name_top.setText(model.topName);
        name_bottom.setText(model.bottomName);
        //要把 iv加入到 container 中
        container.addView(inflate);
        return inflate;
    }

    //销毁条目
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        //移除条目
        container.removeView((View) object);
    }

    public void setArrayList(ArrayList<HomeTopMessageModel> list, MainActivity mainActivity) {
        this.imageData = list;
        context = mainActivity;
    }
}
